var ws, uid, doctorname,davatar, aid, name , sex, height, weight, age, marry, nation, phone, address, spage, wtype;
var newly = 0;
var time, num, eatnotice, runnotice, histor,ztime;
document.addEventListener('plusready',function(){
	uid = plus.storage.getItem('uid');
	doctorname = plus.storage.getItem('name')?plus.storage.getItem('name'):plus.storage.getItem('user');
	davatar = plus.storage.getItem('userAvatar') != 'null'?plus.storage.getItem('userAvatar'):'';//医生头像
	ws = plus.webview.currentWebview();
	aid = ws.aid;
	name = ws.name;
	sex = ws.sex;
	height = ws.height;
	weight = ws.weight;
	age = ws.age;
	marry = ws.marry; 
	nation = ws.nation; 
	phone = ws.phone;
	address = ws.address;
	spage = ws.spage;
	wtype = ws.wtype;
	console.log(aid+" "+wtype);
	sex == 1?sex='女':sex='男';
	marry == 1?marry='已婚':marry='未婚';
	plus.nativeUI.closeWaiting();
	$('.name').val(name);
	$('.sex').val(sex);
	if(age != 0 && age)$('.age').val(age);
	$('.height').val(height);
	$('.weight').val(weight);
	$('.marry').val(marry);
	$('.nation').val(nation);
	$('.phone').val(phone);
	$('.address').val(address);
	$('input[name=radio1]').click(function(){
		newly = $(this).attr('value');
	})
	//服药注意事项
	$("#top-1 li").on('tap',function(){
		$('.history').blur();
		$(".mui-col-xs-3:eq(0) span:eq(0)").text($(this).text());
		time = $(".mui-col-xs-3:eq(0) span:eq(0)").text().trim();
	});	
	$("#top-2 li").on('tap',function(){
		$('.history').blur();
		$(".mui-col-xs-3:eq(1) span:eq(0)").text($(this).text());
		num = $(".mui-col-xs-3:eq(1) span:eq(0)").text().trim();
	});	
	$("#top-3 li").on('tap',function(){
		$('.history').blur();
		$(".mui-col-xs-3:eq(2) span:eq(0)").text($(this).text());
		eatnotice = $(".mui-col-xs-3:eq(2) span:eq(0)").text().trim();
	});
	$("#top-4 li").on('tap',function(){
		$('.history').blur();
		$(".mui-col-xs-3:eq(3) span:eq(0)").text($(this).text());
		runnotice = $(".mui-col-xs-3:eq(3) span:eq(0)").text().trim();
	});
	//点击确认保存病例
	$('.inf-order-add-btn').on('tap', function(){ 
		histor = $('.history').val().trim();
		ztime = $('.time-data').text();
		console.log(newly+" "+histor+" "+ztime+" "+time+" "+num+" "+eatnotice+" "+runnotice);
		if(!histor){
			toast('请填写主诉！');return;
		}
		$('.zhifzzao').show();
	    $('.zhiftc').slideDown();
	})
	//选择拍照添加
	$('.paizhao').on('tap', function(){
		if(!histor){
			toast('请填写主诉！');return;
		}
		goNewPage('free-medicine-zz.html',{
			aid:aid,
			name :name,
			histor :histor,
			ztime :ztime,
			newly :newly,
			time :time,
			num :num,
			eatnotice :eatnotice,
			runnotice :runnotice,
			doctorname :doctorname,
			davatar :davatar,
			wtype :wtype,
			spage :ws.id,
			pid: ws.spage
		});
	})
	//药材库选药
	$('.photo').on('tap', function(){
		$.ajax({
			type:"post",
			url:apiRoot+"?m=home&c=Medicinal&a=addNotes",
			data :{
				uid :aid,
				fid :aid,
				did :uid,
				name :name,
				history :histor,
				ztime :ztime,
				newly :newly,
				time :time,
				num :num,
				eatnotice :eatnotice,
				runnotice :runnotice,
				doctorname :doctorname,
				davatar :davatar
			},
			dataType :'json',
			success :function(data){
				console.log(JSON.stringify(data));
				if(data > 0){
					toast('添加成功');
					if(spage){
						plus.webview.getWebviewById(spage).close();
					}
					goNewPage('free-medicine-list.html', {aid:data,wtype:wtype});
			        setTimeout(function(){
			        	ws.close();
			        },2000)
				}
			},
			error :function(e){
				console.log(JSON.stringify(e));
				errortoast();
			}
		});
	})
	
})